import crypto from "crypto";
import fastifyJwt from "fastify-jwt";
import vconf from "../utils/vconf.js";
import intercept from "./intercept.js";
const config = vconf("config.yml");
const { noCheckAuth = "", jwtkey } = config;

// 定义全局属性
global.api_limit = {}; // global.api_limit[`limit_${raw.url}`]: [30, 15] 30秒15次请求
global.jump_auth = new Map(); // 跳过权限检测
global.add_map = (jump = []) => {
  //路由不检测 jwt权限
  jump.forEach((key) => global.jump_auth.set(key, true));
};
// 初始化执行
global.add_map(noCheckAuth.split(","));

function cryptPwd(msg) {
  const md5 = crypto.createHash("md5");
  return md5.update(msg).digest("hex");
}

export default function (fastify) {
  // md5配置到全局
  fastify.decorate("md5", cryptPwd);
  // JWT令牌
  fastify.register(fastifyJwt, { secret: cryptPwd(jwtkey) });
  // api拦截器
  // intercept(fastify)
}
